package com.linser.gistools.refactoring.geotools.tools;

import com.linser.gistools.utils.GlobalVariables;
import javafx.application.Platform;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;
import java.text.DecimalFormat;

/**
 * @author: linser
 * @Desc: 坐标展示工具
 * @create: 2024-02-22 16:46
 **/
public class CoordinateDisplayTool extends CursorTool {
    private final TextField coordinateText;

    public CoordinateDisplayTool(TextField coordinateLabel) {
        this.coordinateText = coordinateLabel;
    }

    @Override
    public void onMouseMoved(MapMouseEvent ev) {
        if(ev != null){
            DirectPosition2D cord = ev.getWorldPos();
            // 创建 DecimalFormat 对象，并设置格式化模式
            DecimalFormat decimalFormat = new DecimalFormat("#.###");
            //此处不能在主线程频繁调用，不然会造成线程阻塞
            Platform.runLater(() -> {
                coordinateText.setText(decimalFormat.format(cord.x) + ", " + decimalFormat.format(cord.y));
            });
        }
    }
}
